import functools
import unittest


def dependsOn(depend=None):
    """unittest函数依赖装饰器"""

    def decorator(test_func):
        if depend == test_func.__name__:
            raise ValueError(f"{depend} cannot depend on itself")

        @functools.wraps(test_func)
        def wrapper(self, *args, **kwargs):
            result = self._outcome.result
            errors = [error[0]._testMethodName for error in result.errors]
            skipped = [skip[0]._testMethodName for skip in result.skipped]
            failures = [fail[0]._testMethodName for fail in result.failures]
            flag = (depend in failures) or (depend in errors) or (depend in skipped)
            test = unittest.skipIf(flag, f'{depend} failed or error or skipped')(test_func)
            return test(self, *args, **kwargs)

        return wrapper

    return decorator
